﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
namespace DATA
{
   public  class ProductsREP
    {
       MODELGENERALDataContext context = new MODELGENERALDataContext();
        public List<Product> GetAllProducts()
        {
            List<Product> acc = new List<Product>();

            var acounts = from a in context.Products
                          select a;
            foreach (var account in acounts)
            {
                acc.Add(account);
            }
            return acc;
        }

        public List<Product> GetActiveProducts()
        {
            List<Product> acc = new List<Product>();

            var acounts = from a in context.Products
                          where a.isActived == true
                          select a;
            foreach (var account in acounts)
            {
                acc.Add(account);
            }
            return acc;
        }

        public bool CheckDuplicate(int AccountID)
        {
            Product newacc = new Product();

            for (int i = 0; i < (int)(context.Products.Count()); i++)
            {
                Product acc = context.Products.Single(c => c == newacc);
                if (AccountID==acc.productID)
                {
                    return true;
                }




            }
            return false;
        }

        public bool AddProduct(Product newAccount)
        {
            Product newacc = new Product();
            Product acc = context.Products.Single(c => c == newacc);
            if (CheckDuplicate(newAccount.productID) == false)
            {
                context.Products.InsertOnSubmit(acc);

                context.SubmitChanges();
            }

            return true;

        }
        public void deleteProduct(int AccountID)
        {
            var acc = from c in context.Products
                      where c.productID == AccountID
                      select c;

            context.Products.DeleteAllOnSubmit(acc);
            context.SubmitChanges();


        }

        public bool UpdateProduct(Product accountup)
        {
            try
            {
                Product acc = context.Products.Single(
                                                        nv => nv.productID == accountup.productID
                                                        );
                acc.productTypeID = accountup.productTypeID;
                acc.productName = accountup.productName;
               acc.isActived = accountup.isActived;
                acc.productContents = accountup.productContents;
                acc.price = accountup.price;
                acc.panelID = accountup.panelID;
                acc.manufacturerID = accountup.manufacturerID;
                acc.countView = accountup.countView;
                acc.country = accountup.country;
             
                context.SubmitChanges();
            }

            catch (System.Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}
